<?php
/**
 * Created by PhpStorm.
 * User: zyark
 * Date: 2018/3/26
 * Time: 15:24
 */

namespace app\admin\controller;


use app\common\controller\Backend;
use app\common\utils\Constant;

/**
 * 后台ajax获取数据
 * Class Adminpublic
 * @package app\admin\controller
 */
class Adminpublic extends Backend
{
    /**
     * ajax获取分类列表
     * @return \think\response\Json
     */
    public function ajax_category_list(){
        $parent_id = $this->request->param('parent_id');
        $map['parent_id'] = $parent_id;
        $list = model('Category')->where($map)->select();
        return json($list);
    }

    /**
     * ajax获取地区列表
     */
    public function ajaxGetRegionList(){
        $parentId = input('post.parent_id');
        if(empty($parentId) || $parentId == 0){
            $parentId = 0;
        }
        $map['parent_id'] = $parentId;
        $resultList = model('Region')->where($map)->select();
        return json($resultList);
    }

    /**
     * ajax获取地区列表
     */
    public function ajax_get_region_list(){
        $parentId = input('post.parent_id');
        if(empty($parentId) || $parentId == 0){
            $parentId = 0;
        }
        $map['parent_id'] = $parentId;
        $resultList = model('Region')->where($map)->select();
        return json($resultList);
    }

    /**
     * ajax获取小区列表 如果该用户是物业，获取物业下的小区
     * @return \think\response\Json
     */
    public function ajax_village_list(){
        $county_id = $this->request->param('county_id');
        $user_id = $this->auth->id;
        $user = model('User')->where(array('id'=>$user_id))->field('user_type,property_id')->find();
        $map['county_id'] = $county_id;
        if($user['user_type'] == Constant::USER_TYPE_PROPERTY){
            $village_ids = $this->getVillageIds($user['property_id']);
            $map['id'] = ['in',$village_ids];
        }
        $list = model('Village')->where($map)->select();
        return json($list);
    }



    /**
     * 删除文件
     */
    public function ajax_delete_file(){
        $file_path = $this->request->post('file_path');
        if(file_exists($file_path)){
            unlink($file_path);
            $this->success('删除成功！');
        }else{
            $this->success('删除失败！');
        }

    }

    /**
     * 上传文件
     */
    public function upload(){
        $file = $this->request->file('file');
        if($file){
            $fileInfo = $file->getInfo();
            $suffix = strtolower(pathinfo($fileInfo['name'], PATHINFO_EXTENSION));
            $suffix = $suffix ? $suffix : 'file';
            if (!in_array($suffix, ['xls','xlsx'])){
                $this->error('请上传Excel格式的文件！');
            }
            $path = ROOT_PATH . 'public' . DS . 'uploads'.DS.'excel';
            if(!is_dir($path)){
                mkdir($path,0770,true);
            }
            $info = $file->move($path);
            if($info){
                $this->success('上传成功！','',['url' => ROOT_PATH . 'public/uploads/excel/' . $info->getSaveName()]);
            }else{
                // 上传失败获取错误信息
                $this->error($file->getError());
            }
        }else{
            $this->error('文件不存在！');
        }
    }
}